package com.ruoyi.web.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.config.pay.CommonFieldName;
import com.ruoyi.common.config.pay.OrderIdGenerator;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.web.request.IESI123103Request;
import com.ruoyi.web.request.IESI123104Request;
import com.ruoyi.web.service.AgentPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.PaymentApplyResultMapper;
import com.ruoyi.web.domain.PaymentApplyResult;
import com.ruoyi.web.service.IPaymentApplyResultService;
import com.ruoyi.common.core.text.Convert;

/**
 * 代付申请查询Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-04
 */
@Service
public class PaymentApplyResultServiceImpl implements IPaymentApplyResultService 
{
    @Autowired
    private PaymentApplyResultMapper paymentApplyResultMapper;
    @Autowired
    private AgentPayService agentPayService;
    /**
     * 查询代付申请查询
     * 
     * @param sequenceNo 代付申请查询ID
     * @return 代付申请查询
     */
    @Override
    public PaymentApplyResult selectPaymentApplyResultById(String sequenceNo)
    {
        return paymentApplyResultMapper.selectPaymentApplyResultById(sequenceNo);
    }

    /**
     * 查询代付申请查询列表
     * 
     * @param paymentApplyResult 代付申请查询
     * @return 代付申请查询
     */
    @Override
    public List<PaymentApplyResult> selectPaymentApplyResultList(PaymentApplyResult paymentApplyResult)
    {
        return paymentApplyResultMapper.selectPaymentApplyResultList(paymentApplyResult);
    }

    /**
     * 新增代付申请查询
     * 
     * @param paymentApplyResult 代付申请查询
     * @return 结果
     */
    @Override
    public int insertPaymentApplyResult(PaymentApplyResult paymentApplyResult) throws Exception
    {
        IESI123104Request request =new IESI123104Request();
        request.setApplyNo(paymentApplyResult.getApplyNo());
        HashMap<String, Object> resultMap= agentPayService.IESI123104(request,paymentApplyResult.getApplyNo(),paymentApplyResult.getMerchCd());
        String respCode = (String) resultMap.get(CommonFieldName.RESP_CODE);
        String respMsg = (String) resultMap.get(CommonFieldName.RESP_MSG);
        if (!"00000".equals(respCode)){
            BusinessException.notNull(null,respMsg);
        }
        paymentApplyResult.setStatus(respCode);
        paymentApplyResult.setMsg(respMsg);
        paymentApplyResult.setAddInfo((String) resultMap.get("addInfo"));
        if (resultMap.get("amt")!=null){
            paymentApplyResult.setAmt(new BigDecimal((String)resultMap.get("amt")));
        }
        paymentApplyResult.setApplyDate((String) resultMap.get("applyDate"));
        paymentApplyResult.setApplyTime((String) resultMap.get("applyTime"));
        paymentApplyResult.setExtInfo((String) resultMap.get("extInfo"));
        paymentApplyResult.setExtRef((String) resultMap.get("extRef"));
        paymentApplyResult.setExtStat((String) resultMap.get("extStat"));
        paymentApplyResult.setFileId((String) resultMap.get("fileId"));
        paymentApplyResult.setFileName((String) resultMap.get("fileName"));
        paymentApplyResult.setSequenceNo(paymentApplyResult.getApplyNo());
        paymentApplyResult.setMode((String) resultMap.get("mode"));
        PaymentApplyResult old =selectPaymentApplyResultById(paymentApplyResult.getApplyNo());
        if (old!=null){
            return paymentApplyResultMapper.updatePaymentApplyResult(paymentApplyResult);
        }
        return paymentApplyResultMapper.insertPaymentApplyResult(paymentApplyResult);
    }

    /**
     * 修改代付申请查询
     * 
     * @param paymentApplyResult 代付申请查询
     * @return 结果
     */
    @Override
    public int updatePaymentApplyResult(PaymentApplyResult paymentApplyResult)
    {
        return paymentApplyResultMapper.updatePaymentApplyResult(paymentApplyResult);
    }

    /**
     * 删除代付申请查询对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaymentApplyResultByIds(String ids)
    {
        return paymentApplyResultMapper.deletePaymentApplyResultByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除代付申请查询信息
     * 
     * @param sequenceNo 代付申请查询ID
     * @return 结果
     */
    @Override
    public int deletePaymentApplyResultById(String sequenceNo)
    {
        return paymentApplyResultMapper.deletePaymentApplyResultById(sequenceNo);
    }
}
